home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / newsgrp / group98b.txt / 000166_icon-group-sender _Mon Aug 31 09:29:09 1998.msg < prev    next >
Internet Message Format  |  2000-09-20  |  4KB

  1. Return-Path: <icon-group-sender>
  2. Received: from kingfisher.CS.Arizona.EDU (kingfisher.CS.Arizona.EDU [192.12.69.239])
  3.     by baskerville.CS.Arizona.EDU (8.9.1a/8.9.1) with SMTP id JAA00909
  4.     for <icon-group-addresses@baskerville.CS.Arizona.EDU>; Mon, 31 Aug 1998 09:29:08 -0700 (MST)
  5. Received: by kingfisher.CS.Arizona.EDU (5.65v4.0/1.1.8.2/08Nov94-0446PM)
  6.     id AA21342; Mon, 31 Aug 1998 09:28:43 -0700
  7. From: gep2@computek.net
  8. Date: Sat, 29 Aug 1998 01:50:56 -0500
  9. Message-Id: <199808290650.BAA28892@axp.cmpu.net>
  10. Mime-Version: 1.0
  11. Content-Type: text/plain
  12. Content-Transfer-Encoding: 7bit
  13. Subject: Re: A hood fan is guard to mind.
  14. To: icon-group@optima.CS.Arizona.EDU
  15. In-Reply-To: <na.ecc435487c.a600a0ardler@argonet.co.uk>
  16. X-Mailer: SPRY Mail Version: 04.00.06.17
  17. Content-Transfer-Encoding: 7bit
  18. Content-Transfer-Encoding: 7bit
  19. Errors-To: icon-group-errors@optima.CS.Arizona.EDU
  20. Content-Transfer-Encoding: 7bit
  21. Status: RO
  22. Content-Length: 2742
  23.  
  24. > can [this program] be shortened by icon clever stuff. The nested
  25. "every"s seem reminiscent of lesser languages:-
  26.  
  27. Indeed!
  28.  
  29. > procedure main()
  30.   L := ["g","m","h","f"]
  31.   every h:=1 to 4 do {
  32.     every i:=1 to 4 do {
  33.       every j:=1 to 4 do {
  34.         every k:=1 to 4 do {
  35.           write(fit(h,i,j,k))
  36.   }}}}
  37. end
  38. global L,h,i,j,k
  39. procedure fit(a,b,c,d)
  40.   if a=b|a=c|a=d|b=c|b=d|c=d then fail
  41.   return "A "||L[a]||"ood "||L[b]||"an is "||L[c]||"ard to "||L[d]||"ind."
  42. end
  43.  
  44. How about something more like:
  45.  
  46. procedure main()
  47.   L := ['g','m','h','f']
  48.   every write(fit(!L,!L,!L,!L))
  49. end
  50. global L
  51. procedure fit(a,b,c,d)
  52.   if *(a ++ b ++ c ++ d) < 4 then fail
  53.   return "A "|| a ||"ood "|| b ||"an is "|| c ||"ard to "|| d ||"ind."
  54. end
  55.  
  56. Note that L is now a list of character sets rather than character strings!  This 
  57. should reduce the number of conversions necessary.  It also reduces the number 
  58. of character set to string conversions required until the final return statement 
  59. where everything's concatenated into a final string to return.
  60.  
  61. The question is whether the character set union (and size) operation takes less 
  62. time (or more) than the equals tests used in the original program.  (It's 
  63. certainly more Icon-like to use the character set union and size).  Also, why 
  64. convert all the time between subscript numbers and the corresponding characters, 
  65. as the original did?  Icon iterates just great across strings.  
  66.  
  67. In general, character sets are one of the most sadly neglected features of 
  68. Icon... a wonderful feature that deserves to be used more.
  69.  
  70. You might also want to reduce the list, calling and parameter passing overhead 
  71. and move the whole thing into the main procedure, more like:
  72.  
  73. procedure main()
  74.   L = 'gmhf'
  75.   every *((a := !L) ++ (b := !L) ++ (c := !L) ++ (d := !L)) = 4 do
  76.    write("A "|| a ||"ood "|| b ||"an is "|| c ||"ard to "|| d ||"ind.")
  77. end
  78.  
  79. You might even want to take advantage of this kind of structure to use four 
  80. separate character set constants in the character set concatenation, to 
  81. eliminate in advance those words which you know won't be legal words... such as:
  82.  
  83. procedure main()
  84.   every *((a := 'gmhf') ++ (b := 'mf') ++ (c := 'gh') ++ (d := 'mhf')) = 4 do
  85.    write("A "|| a ||"ood "|| b ||"an is "|| c ||"ard to "|| d ||"ind.")
  86. end
  87.  
  88. It's probably possible to use character set differences to extend this principle 
  89. and use the difference set for subsequent generations (if you use the same set 
  90. for all four iterations), which would eliminate the need for the test for 4 
  91. different characters in the resulting set.  But that might go beyond elegance 
  92. and just get obscure.  :-)
  93.  
  94. Gordon Peterson
  95. http://www.computek.net/public/gep2/
  96. Support the Anti-SPAM Amendment!  Join at http://www.cauce.org/
  97.  
  98.